<!--
   Copyright 2024 The Google Research Authors.
  
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
  
       http://www.apache.org/licenses/LICENSE-2.0
  
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
-->

<!DOCTYPE html>
<html>
<head>
<title>Click Button Click Option Task</title>
<!-- stylesheets -->
<link rel="stylesheet" type="text/css" href="../core/core.css">
<link rel="stylesheet" href="../core/jquery-ui/jquery-ui.min.css">
<style>
  input { width: 20px; }
  /* 160 + 160 + 50 + 30 */
  #wrap,
  #sync-task-cover {
    height: 400px;
    width: 160px;
  }

  #click-canvas,
  #reward-display {
    left: 165px;
  }

  #query {
    background-color: #FF0;
    font-size: 10px;
    height: 74px;
    padding: 3px;
  }
</style>

<!-- JS -->
<script src="../core/core.js"></script>
<script src="../core/d3.v3.min.js"></script>
<script src="../common/ui_utils.js"></script>
<script src="../core/jquery-ui/external/jquery/jquery.js"></script>
<script src="../core/jquery-ui/jquery-ui.min.js"></script>

<script>
core.EPISODE_MAX_TIME = 2000000;  // 2000 seconds
var buttons = ['submit', 'okay', 'ok', 'yes', 'no', 'cancel', 'next', 'previous'];

// for click-option
var createElements = function(div){
  var n = core.randi(2, 7);
  var clickNames = [];
  for(var i=0;i<n;i++) {
    var chname = ui_utils.generateString(2,8);
    var label = div.append('label')
    label.append('input').attr('type', 'radio').attr('id', 'ch'+i).attr('name', 'radio');
    label[0][0].innerHTML += chname;
    div.append('br');
    clickNames.push(chname);
  }
  var ix = core.randi(0, n);
  var qstr = clickNames[ix];
  return {query: qstr, index: ix};
}

var genProblem = function () {
  // generate the task
  var div = d3.select('#area');
  div.html(''); // clear previous problem, if any

  var num = 6;
  var elements = [];
  for (var i = 0; i < num; i++) {
    var u = core.randf(0, 1);
    var must_make_button = (i === num - 1) && elements.length === 0;

    if (u < 0.33 && !must_make_button) {
      var txt = ''; for (var q = 0; q < 3; q++) { txt += ' ' + core.sample(ui_utils.lorem_words); }
      div.append('div').html(txt);
    } else if (u < 0.66 && !must_make_button) {

      if (core.randf(0, 1) < 0.5) {
        var txt = ''; for (var q = 0; q < 3; q++) { txt += ' ' + core.sample(ui_utils.lorem_words); }
        txt += ': ';
        div.append('span').html(txt);
      }

      var w = core.randi(40, 150);
      div.append('input').attr('type', 'text').attr('style', 'width:' + w + 'px;');
      div.append('br');
    } else {
      var btn_text = core.sample(buttons);
      if (core.randf(0, 1) < 0.5) btn_text = ui_utils.txtCapitalize(btn_text);
      var btn = div.append('button').html(btn_text);
      if (core.randf(0, 1) < 0.5) div.append('br');
      elements.push(btn);
    }

  }

  var correct_text = core.sample(elements).html();
  var button_state = -1.0
  for (var i = 0, len = elements.length; i < len; i++) {
    var e = elements[i];
    if (e.html() === correct_text) {
      e.on('click', function () { button_state = 1.0; console.log([button_state, correct_text]);})
    } else {
      e.on('click', function () { button_state = -1.0; console.log([button_state, correct_text]);})
    }
  }

  // add element for click-option
  div.append('br')
  div.append('div').attr('id', 'boxes')
  var div_option = d3.select('#boxes');
  div_option.html('');
  var correctElement = createElements(div_option);

  div.append('br')
  div.append('button').html('Submit').attr('id', 'subbtn').attr('class', 'secondary-action');

  // generate query text in the UI
  d3.select('#query').html('Click on the "' + correct_text + '" button, and then select ' + correctElement.query + ' and click Submit.');
  // d3.select('#query').html('Select ' + correctElement.query + ' and click Submit, after clicking on the "' + correct_text + '" button.');

  d3.select('#subbtn').on('click', function(){
    var r = d3.select('#ch'+correctElement.index)[0][0].checked ? 1.0 : -1.0;
    if (button_state == 1.0) {
      core.endEpisode(r, r > 0);
    } else {
      core.endEpisode(-1.0);
    }
  });
}

window.onload = function () {
  core.startEpisode();
}
</script>
</head>
<!-- Base HTML -->
<body>
  <div id="wrap">
    <div id="query"></div>
    <div id="area">
    </div>
  </div>
</body>
</html>
